// server.js
const http = require("http")
const fs = require("fs")
const server = http.createServer(function(req,res){
    res.setHeader("Access-Control-Allow-Origin","null")
    console.log("server index enter")
    if(req.url === "/index"){
        // 服务器接收图片数据的过程
        console.log("server index enter")
        var data = []
        req.on("error",function(err){
            if(err) console.log(err)
        }).on("data",function(chunk){
            data.push(chunk)
        }).on("end",function(){
            data = Buffer.concat(data).toString()
            var base64 = Buffer.from(data,"base64")
            // 将接收到的图片base64编码转换成 demo.png
            fs.writeFile("demo.png",base64,function(){
                console.log("\033[34mdemo.png has been converted to complete!\033[39m")
            })
        })
        res.end("the index have received the base64 code..")
    }
})

server.listen(3000,function(){
    console.log("server is running at http://localhost:3000")
})